package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.utils.SpUtils;
import dhm.com.hpx.utils.StringUtils;

public class ResetActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.pass)
    EditText pass;

    private Drawable drawables;

    private PressenterImpl pressenter;
    private String uid;


    @Override
    protected int getLayout() {
        return R.layout.activity_reset;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);


        uid = SpUtils.getString(this, "uid");

        pass.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
                Drawable drawable = pass.getCompoundDrawables()[2];
                //如果右边没有图片，不再处理
                if (drawable == null) {
                    return false;
                }
                //如果不是按下事件，不再处理
                if (motionEvent.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (motionEvent.getX() > pass.getWidth() - pass.getPaddingRight() - drawable.getIntrinsicWidth()) {
                    showOrHide(pass);
                }
                return false;
            }
        });



    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.back, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:

                String text_pass = pass.getText().toString();
                if (StringUtils.isRegistMobileNO1(text_pass)){
                    Toast.makeText(this, "密码格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }

                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid",uid);
                map.put("new_pass",text_pass);

                pressenter.sendMessage(this, Constant.set_new_pass,map, Bean.class);
                break;
                default:
        }
    }

    private void showOrHide(EditText etPassword) {
        //记住光标开始的位置
        int pos = etPassword.getSelectionStart();
        if (etPassword.getInputType() != (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {//隐藏密码
            drawables = getResources().getDrawable(R.mipmap.eyen);
            drawables.setBounds(0, 0, drawables.getMinimumWidth(), drawables.getMinimumHeight());
            etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {//显示密码
            drawables = getResources().getDrawable(R.mipmap.eyes);
            drawables.setBounds(0, 0, drawables.getMinimumWidth(), drawables.getMinimumHeight());
            etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        etPassword.setCompoundDrawablesWithIntrinsicBounds(null,null, drawables, null);
        etPassword.setSelection(pos);
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                Intent intent=new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    public void fail(String error) {

    }
}
